<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{baseui/head :: head}">
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="动检通智慧云平台"/>
    <meta name="author" content="牧中(上海)物联网科技有限公司"/>
    <link rel="shortcut icon" href="/webjars/images/favicon.png"/>
    <link rel="stylesheet" href="/webjars/js/layui/css/layui.css" media="all">
    <title>牧中畜牧业大数据智慧云平台V1.0</title>


</head>
<style>
    .ui-jqgrid tr.jqgrow td {
        white-space: normal !important;
        height:auto;
    }
</style>
<body>
<!--顶部一级导航-->
<th:block th:replace="baseui/topheader :: topheader(1)"/>

<!--动检通菜单-->
<th:block th:replace="djt/partial/topnav :: topnav(10)"/>


<div id="cl-wrapper">
    <!--左边导航-->
    <th:block th:replace="djt/partial/reportFormLeft :: nav(1)"/>

    <div class="container-fluid" id="pcont">
        <!--<div class="page-head">-->
        <!--<h3>产地检疫日报表</h3>-->
        <!--</div>-->


        <div class="row">
            <div class="col-md-17">
                <div class="block-flat">
                    <div class="header"><h3>产地检疫报表</h3></div>
                    <div class="content">
                        <div class="table-responsive">
                            <div id="datatable_wrapper" class="form-inline dt-bootstrap">

                                <div class="row">

                                    <!-- search-->

                                    <div style="white-space: nowrap">
                                        <div class="row" style="margin-left: 30px">

                                            <div class="from-group">

                                                <div class="col-sm-1">
                                                    <div class="input-group date datetime col-md-4 col-xs-5"
                                                         data-min-view="2" data-date-format="yyyy-mm-dd">
                                                        <input id="start" class="form-control" style="width: 90px;"
                                                               size="16"
                                                               type="text" value="" placeholder="开始时间" readonly>
                                                        <span class="input-group-addon btn btn-primary"><span
                                                                class="glyphicon glyphicon-th"></span></span>
                                                    </div>
                                                </div>
                                                <div class="col-sm-1">
                                                    <div class="input-group date datetime col-md-4 col-xs-5"
                                                         data-min-view="2" data-date-format="yyyy-mm-dd">
                                                        <input id="end" class="form-control" style="width: 90px;"
                                                               size="16"
                                                               type="text" value="" placeholder="结束时间" readonly>
                                                        <span class="input-group-addon btn btn-primary"><span
                                                                class="glyphicon glyphicon-th"></span></span>
                                                    </div>


                                                </div>

                                                <span style="margin-left: 5px">   区域:
                                                    <select class="select2" style="max-width: 120px;"
                                                            onchange="showfactory()" id="city">
                                                        <option value="全部">所有区域</option>
                                                        <span th:each="cityAreaDto :${cityAreaDtoList}">
                                                                <optgroup th:label="${cityAreaDto.city}">
                                                                    <span th:each="region :${cityAreaDto.regions}">
                                                                        <option th:value="${region}"
                                                                                th:text="${region}">南漳县</option>
                                                                    </span>
                                                                   </optgroup>
                                                            </span>
                                                    </select>
                                                </span>
                                                <span style="margin-left: 10px">   畜禽:
                                                    <select class="select2" style="max-width: 120px;"
                                                            onchange="showfactory()" id="breedType">
                                                        <option value="-1">全部</option>
                                                        <option value="1">猪 </option>
                                                        <option value="2">牛 </option>
                                                        <option value="3">羊 </option>
                                                        <option value="4">家禽</option>
                                                    </select>

                                                </span>

                                                <span style="margin-left: 10px">   申报点:
                                                    <select class="select2" style="max-width: 150px;"
                                                            onchange="getDepartment()" id="declareStation">
                                                        <option value="-1">所有申报点</option>
                                                        <span th:each="declareStation :${declareStations}">
                                                            <option th:value="${declareStation.id}"
                                                                    th:text="${declareStation.name}"></option>
                                                        </span>
                                                    </select>
                                                </span>
                                                <span style="margin-left: 40px">
                                                    <button class="btn btn-primary" onclick="SearchBtn()">搜索</button>
                                                </span>
                                                <span style="margin-left: 40px">
                                                    <button class="btn btn-primary" onclick="GetReport()">导出报表</a></button>
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                    <div>
                                        <label style="font-size: 16px;margin-left: 10%;">动物监督所（分所）名称：<label id="department" style="height: 8px;width: 200px"></label></label>
                                        <label style="font-size: 16px;margin-left: 100px;">单位：<label
                                                id="butcherFactory" style="height: 8px;width: 200px">头、只、羽、匹、箱</label></label>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-21" style="width: 1980px">

                                            <!-- Table -->
                                            <table id="jqGrid" style="height: auto" class="ui-jqgrid tr.jqgrow td th.ui-th-column div"></table>
                                            <div id="jqGridPager"></div>

                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>


        </div>
    </div>

</div>

<span style="visibility: hidden">   养殖场:
    <select class="select2"
            id="factory">
        <option value='-1' selected>所有养殖场</option>
        <span th:each=" breedFactory :${breedFactoryList}">
            <option th:text="${breedFactory.name}"
                    th:value="${breedFactory.id}">选项 1</option>
        </span>
    </select>
</span>

<script type="text/javascript" src="/webjars/js/jquery.js"></script>

<script type="text/javascript" src="/webjars/js/jquery.nanoscroller/jquery.nanoscroller.js"></script>
<script type="text/javascript" src="/webjars/js/behaviour/general.js"></script>
<script src="/webjars/js/jquery.ui/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript" src="/webjars/js/jquery.nestable/jquery.nestable.js"></script>

<script type="text/javascript" src="/webjars/js/bootstrap.switch/bootstrap-switch.min.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<script src="/webjars/js/jquery.select2/select2.min.js" type="text/javascript"></script>

<script src="/webjars/js/bootstrap.slider/js/bootstrap-slider.js" type="text/javascript"></script>
<script type="text/javascript" src="/webjars/js/jquery.niftymodals/js/jquery.modalEffects.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.summernote/dist/summernote.min.js"></script>


<script type="text/javascript" src="/webjars/js/jquery.magnific-popup/dist/jquery.magnific-popup.min.js"></script>


<script src="/webjars/js/behaviour/voice-commands.js"></script>
<script src="/webjars/js/bootstrap/dist/js/bootstrap.min.js"></script>
<script type="text/ecmascript" src="/webjars/jqgrid/js/i18n/grid.locale-cn.js"></script>
<script type="text/ecmascript" src="/webjars/jqgrid/js/jquery.jqGrid.min.js"></script>

<script src="/webjars/js/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    var initDate;
    initDate = new Date();
    var params = {
        factoryIds: factoryoption()
    };
    $(document).ready(function () {

        App.init();

        //grid
        $("#jqGrid").jqGrid({
            url: '/djt/report/breedReportsForm1',
            postData: params,
            mtype: "get",
            styleUI: 'Bootstrap',
            datatype: "json",
            autowidth: true,
            shrinkToFit: true,
            colModel: [
                {
                    label: '<span style="text-align: center">货主</span>',
                    name: 'owner',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">申报编号</span>',
                    name: 'reportNumber',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">动物种类</span>',
                    name: 'breedType',
                    align: 'center',
                    formatter: ShowBreeType
                },
                {
                    label: '<span style="text-align: center">检疫数量</span>',
                    name: 'quantity',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">检疫地点</span>',
                    name: 'applyReportName',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">合格数</span>',
                    name: 'qualifiedQuantity',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">《动物检疫合格证明》编号</span>',
                    name: 'quarantineCertNumber',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">不合格数</span>',
                    name: 'unqualifiedQuantity',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">《检疫处理通知单》编号</span>',
                    name: 'harmlessNumber',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">目的地</span>',
                    name: 'receiveAddressValue',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">与载工具牌号</span>',
                    name: 'transportNumber',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">官方兽医姓名</span>',
                    name: 'vetUserName',
                    align: 'center'
                },
                {
                    label: '<span style="text-align: center">备注</span>',
                    name: 'remark',
                    align: 'center'
                }
            ],
            viewrecords: true,
            height: 350,

            rowNum: 20,
            pager: "#jqGridPager"
        });

    });
    function city() {

        var city = $("#city").val();


        if (city === "全部") {
            var array1 = new Array();
            $("#city option").each(function () {  //遍历所有option
                var txt = $(this).val();   //获取option值
                if (txt !== '') {
                    array1.push(txt);  //添加到数组中
                }
            });

            return array1.toString();
        } else {
            var array2 = new Array();
            array2.push(city);
            return array2.toString();
        }

    }

    function showfactory() {


        var type = $("#breedType").val();

        $.ajax({

            url: "/djt/breedarch/search",
            data: {region: city(), breedType: type},
            dataType: "json",
            success: function (data) {

                $("#factory").empty();

                if (data.length != 0) {
                    var str1 = " <option value='-1' selected>所有养殖场</option>";
                    $("#factory").append(str1);
                    $.each(data, function (index, obj) {
                        var id = obj.id;
                        var name = obj.name;
                        var str = "<option value=" + id + ">" + name + "</option>";

                        $("#factory").append(str);
                    });
                } else {

                    var str = "<option value='0' selected>无</option>";
                    $("#factory").append(str);
                }
            }
        });
    }
    function factoryoption() {

        var factory_id = $("#factory").val();


        if (factory_id == -1) {
            var array1 = new Array();
            $("#factory option").each(function () {  //遍历所有option
                var txt = $(this).val();   //获取option值
                if (txt != '') {
                    array1.push(txt);  //添加到数组中
                }
            });

            return array1.toString();
        } else if (factory_id == 0) {
            var array3 = new Array();
            array3.push(factory_id);
            return array3.toString();

        } else {
            var array2 = new Array();
            array2.push(factory_id);
            return array2.toString();
        }

    }
    //搜索
    var SearchBtn = function () {
        var time1 = $("#start").val();
        var time2 = $("#end").val();
        var breedType = $("#breedType").val();
        var area = $("#city").val();
        var declareStationId = $("#declareStation").val();

        if (time1 == "") {
            time1 = new Date("2000-01-01 :00:00:00");
        } else {
            time1 = new Date(time1);
        }

        if (time2 == "") {
            time2 = new Date();
        } else {
            time2 = new Date(time2);
        }

        if (breedType == -1) {
            breedType = null;
        }
        var ids = factoryoption();
        var sdata = {   // 构建查询需要的参数
            start: time1,
            end: time2,
            breedType: breedType,
            factoryIds: ids,
            declareStationId: declareStationId
        };

        var postData = $("#jqGrid").jqGrid("getGridParam", "postData");

        $.extend(postData, sdata);

        $("#jqGrid").jqGrid("setGridParam", {
            search: true    // (6)将jqGrid的search选项设为true
        }).trigger("reloadGrid", [{page: 1}]);   // (7)重新载入Grid表格，以使上述设置生效

    }
    //    //    时间格式化
    //    function ShowDate(cellvalue, options, rowObject) {
    //
    //        var timestamp4 = new Date(cellvalue);
    //        var dt = timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8);
    //        return dt;
    //    }
    //
    function ShowBreeType(cellvalue, options, rowObject) {
        var type;
        switch (cellvalue) {
            case 1:
                type = "猪";
                break;
            case 2:
                type = "牛";
                break;
            case 3:
                type = "羊";
                break;
            case 4:
                type = "家禽";
                break;
        }
        return type;
    }

    function getDepartment() {
        var declareStationId = $("#declareStation").val();

        if(declareStationId === "-1"){
            $("#department").html('');
            return;
        }

        $.ajax({

            url: "/getDepartment",
            data: {declareStationId: declareStationId},
            dataType: "json",
            success: function (data) {
                if (data !== null) {
                    console.info(data);
                    $("#department").html(data.departmentName)
                }else if(data === null || data === undefined || data === ''){
                    console.info(data)
                    $("#department").html('')
                }
            }
        })
    }

    function GetReport() {

        var time1 = $("#start").val();
        var start ;
        var end;
        var time2 = $("#end").val();
        var breedType = $("#breedType").val();
        var declareStationId = $("#declareStation").val();
        var area = $("#city").val();
        var declareStation = "全部";
        if(declareStationId === "-1"){
            declareStation = "全部";
            declareStationId = parseInt(declareStationId);
        }else {
            declareStationId = parseInt(declareStationId);
            $.ajax({

                url: "/getDeclareStation",
                data: {declareStationId: declareStationId},
                dataType: "json",
                success: function (data) {
                    if (data !== null) {
                        console.info(data);
                        declareStation = data.name;
                    }else if(data === null || data === undefined || data === ''){
                        console.info(data)
                        declareStation = "全部";
                    }
                }
            });
        }


        if (time1 === "") {
            time1 = initDate.getFullYear() + "-1-1";
            start = new Date(time1);
        }else {
            start = new Date(time1);
        }

        if (time2 === "") {
            time2 = initDate.getFullYear() + "-" + initDate.getMonth() + "-" + initDate.getDay();
            end = new Date();
        }else {
            end = new Date(time2);
        }

        var type = "全部";
        if(breedType === "1"){
            type = "猪";
        }else if(breedType === "2"){
            type = "牛";
        }else if(breedType === "3"){
            type = "羊";
        }else if(breedType === "4"){
            type = "家禽";
        }

        var breedFactoryIds = factoryoption();

        var href = "/getExcel";

        if(breedType === "-1"){
            breedType = parseInt(breedType);
        }else {
            breedType = parseInt(breedType);
        }

        href = href + "/" + start + "/" + end + "/" + breedType + "/" + breedFactoryIds + "/" + declareStationId;

        console.info(type);
        layui.use(['laypage', 'layer'], function () {

            var $ = layui.jquery, layer = layui.layer;
            layer.open({
                type: 2,
                title: '产地检疫报表',
                skin: 'layui-layer-lan',
                area: ['750px', '450px'],
                content: ['/djt/report/formModel', 'no'],
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];

                    body.find("#start").val(time1);
                    body.find("#end").val(time2);
                    body.find("#area").val(area);
                    body.find("#breedType").val(type);
                    body.find("#declareStation").val(declareStation);
                    body.find("#cancel").click(function () {
                        layer.close(index);
                    });
                    var a = body.find("#download");

                    a.attr("href",href);
                    a.click(function () {
                        layer.close(index);
                    });

                }
            })
        })

    }

</script>

</body>
</html>
